# -*- coding: utf-8 -*-
from igetui.template.igt_transmission_template import *
from igetui.template.igt_link_template import LinkTemplate
from igetui.igt_message import *
from igt_push import IGeTui
from igetui.igt_target import *
from tornado import gen
from tornado.ioloop import IOLoop
from common.dbutils import MySQLDBUtils
import time

import json
from common import timetool
from payload.APNPayload import SimpleAlertMsg

APPID = 'orA7ZTYdcH8NnZ27rsfHP3';
APPKEY = 'xlRkab5luvA0NfxEZopFj9';
MASTERSECRET = 'O6Fiu3d9du8UN9N77e1My7';
HOST = 'http://sdk.open.api.igexin.com/apiex.htm'


MYSQL_APP_HOST = '47.92.112.59'
MYSQL_APP_DBNAME = 'qicheng'
MYSQL_APP_USERNAME = 'qic'
MYSQL_APP_PASSWORD = 'qic2017.,/'

# 客户端app连接配置
mysql_conn_app_dict = dict(host=MYSQL_APP_HOST, port=3306,
                           user=MYSQL_APP_USERNAME,
                           pwd=MYSQL_APP_PASSWORD,
                           db=MYSQL_APP_DBNAME, charset='utf8',
                           max_idle_connections=10,
                           max_open_connections=100, max_recycle_sec=3)
mysql_app_pool = MySQLDBUtils(mysql_conn_app_dict)

@gen.coroutine
def get_push_info_0(info_id):
    sql = (
        "select a.*, b.client_id, c.nick_name, c.avatar from app_circle_reply a, app_clientids b, app_user_info c where a.id = {} "
        "and a.to_phone = b.phone "
        "and a.phone = c.phone"
    )
    res = yield mysql_app_pool.get_one(sql.format(info_id), cursor='dict')
    raise gen.Return(res)


def TransmissionTemplateDemo(info_dict):
    print info_dict
    template = TransmissionTemplate()
    template.transmissionType = 1
    template.appId = APPID
    template.appKey = APPKEY
    template.transmissionContent = json.dumps(info_dict, ensure_ascii=False)
    #设置APNs信息
    apnpayload = APNPayload()
    apnpayload.sound = ""
    apnpayload.contentAvailable = 0
    apnpayload.category = ""

    #简单类型如下设置
    alertMsg = SimpleAlertMsg()
    alertMsg.alertMsg = "";
    #字典类型如下设置
    alertMsg = DictionaryAlertMsg()
    alertMsg.body = 'body'
    #alertMsg.actionLocKey = 'actionLockey'
    #alertMsg.locKey = 'lockey'
    #alertMsg.locArgs=['loc-args']
    #alertMsg.launchImage = 'launchImage'
    # iOS8.2以上版本支持
    # alertMsg.title = 'Title'
    #alertMsg.titleLocArgs = ['TitleLocArg']
    #alertMsg.titleLocKey = 'TitleLocKey'

    #可以设置字典类型AlertMsg和简单类型AlertMsg其中之一
    apnpayload.alertMsg=alertMsg

    template.setApnInfo(apnpayload)

    return template

def push_message_to_single(info_dict, client_id):
    push = IGeTui(HOST, APPKEY, MASTERSECRET)

    # 新建一个推送模版, 以链接模板为例子，就是说在通知栏显示一条含图标、标题等的通知，用户点击可打开您指定的网页
    template = TransmissionTemplateDemo(info_dict)

    message = IGtListMessage()
    message.data = template
    message.isOffline = True  # 用户当前不在线时，是否离线存储,可选
    message.offlineExpireTime = 6 * 3600 * 1000  # 离线有效时间，单位为毫秒
    target = Target()
    target.appId = APPID
    target.clientId = client_id

    ret = push.pushMessageToSingle(message, target)
    print ret
    return ret




@gen.coroutine
def main():
    info_dict = yield get_push_info_0(1)
    print info_dict
    if info_dict:
        info_dict['push_type'] = 0
    push_message_to_single(info_dict, 'b505fcfba56b8965c13a5d9d2ac7f90a')


if __name__ == "__main__":
    IOLoop.current().run_sync(main)
